package com.td.evo.obj.factory;

import java.util.ArrayList;
import java.util.List;

import com.krakers.evo.log.EvolutionLogic;
import com.krakers.evo.obj.DNA;
import com.krakers.evo.obj.factory.PopulationFactory;
import com.td.evo.log.TDEvolutionLogic;

public class TDFactory extends PopulationFactory {

	private static final int S1 = 225;
	private static final int S2 = 450;
	
	private DNA template;
	
	public TDFactory(DNA template) {
		this.template = template;
	}
	
	@Override
	public DNA createSpecimen() {
		DNA dna = new DNA(S2);
		init(dna);
		return dna;
	}
	
	protected void init(DNA dna) {
		
		for(int i = 0; i < S1; i++) {
			if(template.getGene(i) != 0) {
				dna.setGene(i,1 + EvolutionLogic.r.nextInt(TDEvolutionLogic.TOWERS));
			}
		}
		
		List<Integer> list = new ArrayList<Integer>();
		//parzyste
		for(int i = 0; i < S1; i+=2) {
			list.add(i);
		}
		
		for(int i = 0; i < S1; i+=2) {
			int indx = EvolutionLogic.r.nextInt(list.size());
			dna.setGene(S1+i, list.get(indx));
			list.remove(indx);
		}
		
		//nieparzyste
		for(int i = 1; i < S1; i+=2) {
			list.add(i);
		}
		
		for(int i = 1; i < S1; i+=2) {
			int indx = EvolutionLogic.r.nextInt(list.size());
			dna.setGene(S1+i, list.get(indx));
			list.remove(indx);
		}
	}
	
	public DNA createSpecimen(String str) {
		DNA dna = new DNA(S2);
		
		for(int i = 0; i < dna.getSize(); i++) {
			int indx = str.indexOf(" ");
			int v = Integer.parseInt(str.substring(0, indx));
			str = str.substring(indx+1);
			dna.setGene(i, v);
		}
		
		return dna;
	}
}
